package gitee.derumin.lifeissohard.item;

import gitee.derumin.lifeissohard.lishhungersystem.LishHungerManager;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;


import java.util.List;
import java.util.UUID;


public class BloodTester extends Item {
    private static final UUID TEMP_KNOCKBACK_RESISTANCE_ID = UUID.fromString("8960ba3e-aee6-48ae-bb41-b7db53734e0b");
    private static final UUID TEMP_ATTACK_DAMAGE_ID = UUID.fromString("8960ba3e-aee6-48ae-bb41-b7db53734e0c");
    private static final UUID TEMP_ARMOR_ID = UUID.fromString("8960ba3e-aee6-48ae-bb41-b7db53734e0d");

    public BloodTester(Settings settings){
        super(settings);
    }

    @Override
    public void appendTooltip(ItemStack itemStack, @Nullable World world, List<Text> tooltip, TooltipContext tooltipContext) {
        tooltip.add(Text.translatable("item.lifeissohard.blood_tester.tooltip1").formatted(Formatting.DARK_PURPLE));
        tooltip.add(Text.translatable("item.lifeissohard.blood_tester.tooltip2").formatted(Formatting.DARK_PURPLE));
    }


    @Override
    public int getMaxUseTime(ItemStack stack) {
        return 72000;
    }

    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
        ItemStack itemStack = user.getStackInHand(hand);
        ItemStack chestStack = user.getEquippedStack(EquipmentSlot.CHEST);
        if (chestStack.isOf(Items.ELYTRA) || chestStack.isOf(Items.AIR)) {
            EntityAttributeInstance entityAttributeInstance = user.getAttributeInstance(EntityAttributes.GENERIC_KNOCKBACK_RESISTANCE);
            EntityAttributeInstance entityAttributeInstance2 = user.getAttributeInstance(EntityAttributes.GENERIC_ATTACK_DAMAGE);
            EntityAttributeInstance entityAttributeInstance3 = user.getAttributeInstance(EntityAttributes.GENERIC_ARMOR);
            assert entityAttributeInstance != null;
            entityAttributeInstance.addTemporaryModifier(new EntityAttributeModifier(TEMP_KNOCKBACK_RESISTANCE_ID, "temp knock back resistance", 1.0d, EntityAttributeModifier.Operation.ADDITION));
            assert entityAttributeInstance2 != null;
            entityAttributeInstance2.addTemporaryModifier(new EntityAttributeModifier(TEMP_ATTACK_DAMAGE_ID, "temp attack damage", (1.0f-user.getAttributeValue(EntityAttributes.GENERIC_ATTACK_DAMAGE)), EntityAttributeModifier.Operation.ADDITION));
            assert entityAttributeInstance3 != null;
            entityAttributeInstance3.addTemporaryModifier(new EntityAttributeModifier(TEMP_ARMOR_ID, "temp armor", (0.0f-user.getAttributeValue(EntityAttributes.GENERIC_ARMOR)), EntityAttributeModifier.Operation.ADDITION));
            user.hurtTime = 0;
            user.attack(user);
            entityAttributeInstance.removeModifier(TEMP_KNOCKBACK_RESISTANCE_ID);
            entityAttributeInstance2.removeModifier(TEMP_ATTACK_DAMAGE_ID);
            entityAttributeInstance3.removeModifier(TEMP_ARMOR_ID);
            user.setCurrentHand(hand);
        }
        else return TypedActionResult.fail(itemStack);
        return TypedActionResult.consume(itemStack);
    }

    @Override
    public void onStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks) {
        if (user instanceof PlayerEntity playerEntity) {
            ItemStack stack2 = new ItemStack(LishItems.BLOOD_TESTER_USED, 1);
            if (this.getMaxUseTime(stack)-remainingUseTicks >= 100){
                writeNbt(stack2,world,playerEntity);
            }
            else {
                writeNbtFailed(stack2);
            }
            if (!playerEntity.getAbilities().creativeMode && stack.getCount() == 1) {
                playerEntity.getInventory().removeOne(stack);
            }else if (!playerEntity.getAbilities().creativeMode) {
                stack.decrement(1);
            }
            playerEntity.dropItem(stack2, false);
        }
    }

    private void writeNbt(ItemStack stack, World world, PlayerEntity tester){
        NbtCompound tag = stack.getOrCreateNbt();
        tag.putBoolean(BloodTesterUsed.TEST_SUCCESS,true);
        tag.putString(BloodTesterUsed.TEST_TIME,("day"+(world.getTimeOfDay()/24000+1)+", "+(world.getTimeOfDay()%24000/1000+6)+":"+((int)(world.getTimeOfDay()%1000/16.66667f))));
        tag.putString(BloodTesterUsed.TESTED_PERSON,tester.getEntityName());
        if (tester.getHungerManager() instanceof LishHungerManager lishHungerManager) {
            tag.putString(LishHungerManager.HEAVY_METAL, Integer.toString(lishHungerManager.getHeavyMetal()));
            tag.putString(LishHungerManager.BLOOD_SUGAR, Integer.toString(lishHungerManager.getBloodSugar()));
            tag.putString(LishHungerManager.BLOOD_FAT, Integer.toString(lishHungerManager.getBloodFat()));
            tag.putString(LishHungerManager.VA, Integer.toString(lishHungerManager.getVA()));
            tag.putString(LishHungerManager.VB, Integer.toString(lishHungerManager.getVB()));
            tag.putString(LishHungerManager.VC, Integer.toString(lishHungerManager.getVC()));
            tag.putString(LishHungerManager.IODINE, Integer.toString(lishHungerManager.getIodine()));
        }
    }

    private void writeNbtFailed(ItemStack stack){
        NbtCompound tag = stack.getOrCreateNbt();
        tag.putBoolean(BloodTesterUsed.TEST_SUCCESS,false);
    }

}
